using System;
using System.Text;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace SBPweb.Logging
{
    /// <summary>
    /// Log4Net name provider implementation.
    /// </summary>
    public class Log4NetNameProvider : ILogNameProvider
    {
        #region ILogNameProvider private Members

		private static string mLog4NetNameTemplate = @"{LOGFOLDER}\{EXENAME}#{GUID}_{MACHINENAME}_{DATE}.log.xml";

		#endregion 

		#region ILogNameProvider public Members

		/// <summary>
		/// Returns the name template of the Log4NetNameProvider
		/// </summary>
		public static string Log4NetNameTemplate
		{
			get { return mLog4NetNameTemplate; }
			set	{ mLog4NetNameTemplate = value; }
		}

		/// <summary>
        /// Generates a new absolute file name in the folder controlled by a <see cref="LogFolder"/>.
        /// </summary>
        /// <param name="LogFolder">The <see cref="LogFolder"/> to use as destination.</param>
        /// <returns>An absolute file name.</returns>
        public string GenerateName(LogFolder LogFolder)
        {
            if (LogFolder == null)
                throw new ArgumentNullException("LogFolder");

			StringBuilder tb = new StringBuilder(mLog4NetNameTemplate);
			tb.Replace("{LOGFOLDER}", LogFolder.Folder);
			tb.Replace("{EXENAME}", Path.GetFileName(Environment.GetCommandLineArgs()[0]).Replace(".vshost", ""));
			tb.Replace("{GUID}", Guid.NewGuid().ToString());
			tb.Replace("{MACHINENAME}", Environment.MachineName);
			tb.Replace("{DATE}", DateTime.Now.Date.ToString("MM-dd-yyyy"));
			tb.Replace("{TIME}", DateTime.Now.ToString("MM-dd-yyyy.HH.mm.ss.fff"));

			return tb.ToString();
        }

        #endregion
    }
}